package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.Evento;

public class EventoDAOImpl extends JdbcDaoSupport implements IEventoDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;
    
    private FindByRadicado findByRadicado;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM REG_EVENTO_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(Evento evento) {
		deleteOperation.execute( evento  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public Evento findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(Evento evento) {
		insertOperation.execute( evento );
	}

	public void update(Evento evento) {
		updateOperation.execute( evento );
	}

	public Evento findByCriteria(Evento  evento) {
		return findByCriteriaQuery.runQuery(evento);
	}

	public List load() {
		return findAllQuery.runQuery();
	}
	
	public List loadByRadicado(Evento evento) {
		return findByRadicado.runQuery(evento);
	}

	public Evento findByActiveState(Evento  evento) {
		return findByActiveStateQuery.runQuery(evento);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
   // 	deleteOperation = new DeleteOperation(  getDataSource() );
   // 	deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByRadicado = new FindByRadicado(getDataSource());
    //	findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO REG_EVENTO_T ("
				  + " ID, "
				  + " ID_SOLICITUD, "
				  + " ID_TIPO_EVENTO, "
				  + " DIRECCION, "
				  + " CODIGO_BARRIO, "
				  + " BARRIO, "
				  + " CODIGO_COMUNA, "
				  + " COMUNA, "
				  + " FECHA_INICIO, "
				  + " FECHA_FIN, "
				  + " DESCRIPCION, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " REG_EVENTO_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(Evento evento) {
			InsertOperation.this.update( new Object[]{
				evento.getIdSolicitud(),
				evento.getIdTipoEvento(),
				evento.getDireccion(),
				evento.getCodigoBarrio(),
				evento.getBarrio(),
				evento.getCodigoComuna(),
				evento.getComuna(),
				evento.getFechaInicio(),
				evento.getFechaFin(),
				evento.getDescripcion(),
				evento.getEstado(),
				evento.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE REG_EVENTO_T SET "
					  + " ID_SOLICITUD = ? ,"
					  + " ID_TIPO_EVENTO = ? ,"
					  + " DIRECCION = ? ,"
					  + " CODIGO_BARRIO = ? ,"
					  + " BARRIO = ? ,"
					  + " CODIGO_COMUNA = ? ,"
					  + " COMUNA = ? ,"
					  + " FECHA_INICIO = ? ,"
					  + " FECHA_FIN = ? ,"
					  + " DESCRIPCION = ? ,"
					  + " ESTADO = ? ,"					  
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
					  + WHERE
					  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(Evento evento) {
			UpdateOperation.this.update( new Object[]{
				evento.getIdSolicitud(),
				evento.getIdTipoEvento(),
				evento.getDireccion(),
				evento.getCodigoBarrio(),
				evento.getBarrio(),
				evento.getCodigoComuna(),
				evento.getComuna(),
				evento.getFechaInicio(),
				evento.getFechaFin(),
				evento.getDescripcion(),
				evento.getEstado(),				
				evento.getUsuarioModifica(),
				evento.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM REG_EVENTO_T "
										+ WHERE
			  	  	  					+ " CODIGO = ? ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
		}
		public void execute(Evento evento) {
			this.update( new Object[]{
				evento.getId()
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM REG_EVENTO_T "
										+ WHERE + "1=1";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			Evento evento = new Evento();
				evento.setId(new Long(rs.getLong("ID")));
				evento.setIdSolicitud(new Long(rs.getLong("ID_SOLICITUD")));
				evento.setIdTipoEvento(new Long(rs.getLong("ID_TIPO_EVENTO")));
				evento.setIdTipoEventoStr(rs.getString("ID_TIPO_EVENTO"));
				evento.setDireccion(rs.getString("DIRECCION"));
				evento.setCodigoBarrio(rs.getString("CODIGO_BARRIO"));
				evento.setBarrio(rs.getString("BARRIO"));
				evento.setCodigoComuna(rs.getString("CODIGO_COMUNA"));
				evento.setComuna(rs.getString("COMUNA"));
				evento.setFechaInicio(rs.getDate("FECHA_INICIO"));		
				evento.setFechaFin(rs.getDate("FECHA_FIN"));		
				evento.setDescripcion(rs.getString("DESCRIPCION"));
				evento.setEstado(rs.getString("ESTADO"));
				evento.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				evento.setFechaCrea(rs.getDate("FECHA_CREA"));		
				evento.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				evento.setFechaModifica(rs.getDate("FECHA_MODIFICA"));	
			return evento;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public Evento runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (Evento) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT + WHERE + " 1=1 "
				+ " AND ID_SOLICITUD = ? " + "";

    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public Evento runQuery(Evento evento) {
			List result =  execute( new Object[]{
					evento.getIdSolicitud()
				});
			if ( result.size() > 0 ) {
        		return (Evento) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);    		
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindByRadicado extends BaseQueryOperation {
		private static final String sql = "SELECT EVENTO.*,RADICADO FROM REG_EVENTO_T EVENTO INNER JOIN"
										+ " ADM_SOLICITUD_T SOL ON EVENTO.ID_SOLICITUD=SOL.ID"
										+ WHERE +  " RADICADO =?  ";
    	public FindByRadicado(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(Evento evento) {
    		List result =  execute( new Object[]{    				
    					evento.getId()
    		});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    
    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public Evento runQuery(Evento evento) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (Evento) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
